package cate.game.pvp.championrace.data;

import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import lombok.Getter;
import lombok.Setter;

import java.util.Objects;
import java.util.StringJoiner;

@NoteClass(value = "跨服冠军赛节点信息")
@Setter
@Getter
public class ChampionRaceSpec implements Comparable<ChampionRaceSpec> {
    @NoteField(value = "赛季")
    private int season;
    @NoteField(value = "赛段",detail = "选拔赛1，淘汰赛2，决赛3")
    private byte period;
    private byte group;
    private byte round;
    private byte step;

    public static ChampionRaceSpec ofTrial(int season, byte round) {
        return of(season, (byte) 1, (byte) 0, round);
    }

    public static ChampionRaceSpec ofKo(int season, byte group, byte round) {
        return of(season, (byte) 2, group, round);
    }

    public static ChampionRaceSpec ofFinal(int season, byte round) {
        return of(season, (byte) 3, (byte) 0, round);
    }

    public static ChampionRaceSpec of(int season, byte period) {
        return of(season, period, (byte) 0, (byte) 0);
    }

    public static ChampionRaceSpec of(int season, byte period, byte group, byte round) {
        return of(season, period, group, round, (byte) 0);
    }

    public static ChampionRaceSpec of(int season, byte period, byte group, byte round, byte step) {
        ChampionRaceSpec spec = new ChampionRaceSpec();
        spec.setSeason(season);
        spec.setPeriod(period);
        spec.setGroup(group);
        spec.setRound(round);
        spec.setStep(step);
        return spec;
    }

    public static ChampionRaceSpec ofNullStep(ChampionRaceSpec spec) {
        return of(spec.season, spec.period, spec.group, spec.round);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        ChampionRaceSpec that = (ChampionRaceSpec) o;
        return season == that.season
                && period == that.period
                && group == that.group
                && round == that.round
                && step == that.step;
    }

    @Override
    public int hashCode() {
        return Objects.hash(season, period, group, round, step);
    }

    @Override
    public int compareTo(ChampionRaceSpec that) {
        return Integer.compare(this.sum(), that.sum());
    }

    @JsonIgnore
    private int sum() {
        return season * 10000 + period * 1000 + group * 100 + round * 10 + step;
    }

    @Override
    public String toString() {
        return new StringJoiner("-", "", "")
                .add("" + season)
                .add("" + period)
                .add("" + group)
                .add("" + round)
                .add("" + step)
                .toString();
    }

    public ChampionRaceSpec copy() {
        ChampionRaceSpec copy = new ChampionRaceSpec();
        copy.season = this.season;
        copy.period = this.period;
        copy.group = this.group;
        copy.round = this.round;
        copy.step = this.step;
        return copy;
    }
}
